# -*- coding=utf-8-*-
"""
@File:memory_selection
@Author:Created by Han X.Y
@Date:on 2021/7/12 17:46 
"""

"""
给定一个字符串，找到它的第一个不重复的字符，并返回它的索引。
如果不存在返回-1
"""

from collections import Counter

# class Solution:
#     def firstUniqChar(self, s) :
#         #首先统计频次
#         freqs=Counter(s)
#         for num,i in enumerate(list(s)):
#             if freqs[i]==1:
#                 return num
#         else:
#             return -1


class Solution:
    def firstUniqChar(self, s) :
        """
        使用哈希表
        """
        hash_dict={}
        #先做词频统计
        for i in s:
            hash_dict[i]=hash_dict.get(i,0)+1
        print(hash_dict)

        for num,i in enumerate(s):
            if hash_dict[i]==1:
                return num
        else:
            return -1








if __name__=='__main__':
    s='leetcode'
    s2='loveleetcode'
    S=Solution()
    print(S.firstUniqChar(s))
    print(S.firstUniqChar(s2))